package com.qjs.shiro.service;

import java.util.List;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qjs.shiro.dao.ResourceDao;
import com.qjs.shiro.dao.RoleDao;
import com.qjs.shiro.dao.UserDao;
import com.qjs.shiro.kit.ShiroKit;
import com.qjs.shiro.model.Resource;
import com.qjs.shiro.model.User;

@Service
public class UserService {
	
	@Autowired
	public UserDao userDao;
	@Autowired
	public ResourceDao resourceDao;
	
	@Autowired
	public RoleDao roleDao;
	
	public User login(String username, String password) {
		User u = userDao.getByName(username);
		if(u==null) throw new UnknownAccountException("用户名或者密码出错");
		if(!u.getPassword().equals(ShiroKit.md5(password, username)))
			throw new IncorrectCredentialsException("用户名或者密码出错");
		if(u.getStatus()==0) throw new LockedAccountException("用户已经被锁定");
		return u;
	}

	public List<String> listRoleSnByUser(int uid) {
		
		return roleDao.listRoleSnByUser(uid);
	}

	public List<Resource> listAllResource(int uid) {
		
		return resourceDao.listAllResource(uid);
	}
	
}
